import Vue from 'vue'
import VueRouter from 'vue-router'
//引入Login组件
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import HistoryAdd from '../components/history/HistoryAdd.vue'
import HistoryCase from '../components/history/HistoryCase.vue'
import HistoryList from '../components/history/HistoryList.vue'
import ProjectAdd from '../components/project/ProjectAdd.vue'
import ProjectEdit from '../components/project/ProjectEdit.vue'
import ProjectList from '../components/project/ProjectList.vue'
import RiskDisc from '../components/risk/RiskDisc.vue'
import RiskFenXi from '../components/risk/RiskFenXi.vue'
import RiskGaiTu from '../components/risk/RiskGaiTu.vue'
import RiskGenZong from '../components/risk/RiskGenZong.vue'
import RiskYuJing from '../components/risk/RiskYuJing.vue'
import RiskYuAn from '../components/risk/RiskYuAn.vue'
import RiskFenxi_A from '../components/risk/RiskFenxi_A.vue'
import UserAdmin from '../components/user/UserAdmin.vue'
import Adds from '../components/user/Adds.vue'
import Register from '../components/Register.vue'

Vue.use(VueRouter)

const routes = [
  {
	  path:"/",
	  redirect:"/login"
  },
  {
	  path:"/login",
	  component: Login
  },
  {
  	path: "/register",
  	component: Register
  },
  {
	  path:"/home",
	  component: Home,
	  redirect:"/welcome",
	  children:[
		  {path:"/welcome",component: Welcome,},
		  {path:"/history_add",component: HistoryAdd,},
		  {path:"/history_list",component: HistoryList,},
		  {path:"/history_case",component: HistoryCase ,},
		  {path:"/project_add",component: ProjectAdd,},
		  {path:"/project_edit",component: ProjectEdit,},
		  {path:"/project_list",component: ProjectList,},
		  {path:"/risk_disc",component: RiskDisc,},
		  {path:"/risk_fenxi",component: RiskFenXi,},
		  {path:"/risk_gaitu",component: RiskGaiTu,},
		  {path:"/risk_genzong",component: RiskGenZong,},
		  {path:"/risk_yujing",component: RiskYuJing,},
		  {path:"/risk_yuan",component: RiskYuAn,},
		  {path:"/user_admin",component: UserAdmin,},
		  {path:"/user_adds",component: Adds,},
			{path:"/risk_fenxi_a",component: RiskFenxi_A,},
		  
	  ]
  }
]

const router = new VueRouter({
  routes
})


// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

//路由导航守卫：保护路由访问的方法
/*router.beforeEach((to, from, next) => {
	//to 想要去
	//from 从哪访问
	//next:之后要做的任务，是一个函数  next（）放行，继续访问to的路径， next（'/URL'）强制跳转的路径。
	
	if (to.path === '/login' ){
		return next();
	} 
	
	//从session中调取token
	//并进行是否有值的判断
	//有值=>向下跳转
	//无值=>路由向登录页面跳转
	var userFlag = window.sessionStorage.getItem("user");
	if(userFlag) {
		return next('/login');//无值，返回登录
	}
	return next();
})
*/
//路由导航守卫：保护路由访问的方法
router.beforeEach((to, from, next) => {
	//to 想要去
	//from 从哪访问
	//next:之后要做的任务，是一个函数  next（）放行， next（'/URL'）强制跳转的路径。
	
	if (to.path === '/login' || to.path === '/register') return next();
	
	//从session中调取token
	//并进行是否有值的判断
	//有值=>向下跳转
	//无值=>路由向登录页面跳转
	const tokenStr = window.sessionStorage.getItem("user");
	if(!tokenStr) return next('/login');
	next();
})
export default router